<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.util.List"%>
<%@ page import="javax.jdo.PersistenceManager"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@ page import="com.yeepal.mygae.guestbook.Greeting"%>
<%@ page import="com.yeepal.mygae.guestbook.PMF"%>

<html>
<head>
<link type="text/css" rel="stylesheet" href="Guestbook.css">
<title>guestbook</title>
</head>
<body>

<%
	UserService userService = UserServiceFactory.getUserService();
	String thisURL = request.getRequestURI();
	User user = userService.getCurrentUser();
	//if (user != null) {
	if (userService.isUserLoggedIn()) {

%>
<p>Hello, <%=request.getUserPrincipal().getName()%>! (You can <a
	href="<%=userService.createLogoutURL(request.getRequestURI())%>">sign
out</a>.)</p>

<%
	PersistenceManager pm = PMF.get().getPersistenceManager();
	String query = "select from " + Greeting.class.getName();
	List<Greeting> greetings = (List<Greeting>) pm.newQuery(query)
			.execute();
	if (greetings.isEmpty()) {
%>
<p>The guestbook has no messages.</p>
<%
	} else {
		for (Greeting g : greetings) {
			if (g.getAuthor() == null) {
%>
<p>An anonymous person wrote:</p>
<%
	} else {
%>
<p><b><%=g.getAuthor().getNickname()%></b> wrote:</p>
<%
	}
%>
<blockquote><%=g.getContent()%></blockquote>
<%
	}
	}
	pm.close();
%>
<form action="/sign" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Post Greeting" /></div>
</form>
<%
	} else {
%>
<p>Hello! <a
	href="<%=userService.createLoginURL(request.getRequestURI())%>">Sign
in</a> to include your name with greetings you post.</p>
<%
	}
%>





</body>
</html>